package com.hanthink.web.controller.app.manhourfact;


import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.core.text.Convert;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.manhour.FactWorkerUsed;
import com.hanthink.system.domain.user.User;
import com.hanthink.system.service.manhour.IFactWorkerUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/manhourfact/temporarywork")
public class TemporaryworkControll extends BaseController {
    @Autowired
    private IFactWorkerUsedService factWorkerUsedService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查詢临时用工明细,根据id
     */
    @PostMapping("/queryid")
    public TableDataInfo querytemporaryworkid(@RequestBody Map<String, Object> map) throws ParseException {
        long idinx = Convert.toLong(map.get("id").toString());
        FactWorkerUsed listone = factWorkerUsedService.selectFactWorkerUsedById(idinx);
        List<FactWorkerUsed> list=new ArrayList<FactWorkerUsed>();
        list.add(listone);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }
    /**
     * 查詢临时用工明细
     */
    @PostMapping("/query")
    public TableDataInfo querytemporarywork(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        factWorkerUsed.setCompanyid(map.get("companyid").toString());
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        date = formatter.parse(map.get("planworkdatetime").toString().replace("/","-"));
        factWorkerUsed.setPlanworkdatetime(date);

        if (map.get("pshiftid").toString().equals("1")){
            factWorkerUsed.setPshiftid("白班");
        }
        else if(map.get("pshiftid").toString().equals("2")){
            factWorkerUsed.setPshiftid("夜班");
        }
        else if(map.get("pshiftid").toString().equals("3")){
            factWorkerUsed.setPshiftid("中班");
        }
        else if(map.get("pshiftid").toString().equals("4")){
            factWorkerUsed.setPshiftid("转班(白)");
        }
        else if(map.get("pshiftid").toString().equals("5")){
            factWorkerUsed.setPshiftid("转班(夜)");
        }

        switch (map.get("workertype").toString())
        {
            case "返工挑选":
                factWorkerUsed.setWorkertype("L001");
                break;
            case "设备临时损坏或停机":
                factWorkerUsed.setWorkertype("L002");
                break;
            case "临时性清洁工作":
                factWorkerUsed.setWorkertype("L003");
                break;
            case "临时离厂":
                factWorkerUsed.setWorkertype("L004");
                break;
            case "试模打样":
                factWorkerUsed.setWorkertype("L006");
                break;
            case "在线巡检":
                factWorkerUsed.setWorkertype("L007");
                break;
            case "其他":
                factWorkerUsed.setWorkertype("L005");
                break;
        }

        List<FactWorkerUsed> list = factWorkerUsedService.selectFactWorkerUsedList(factWorkerUsed);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    /**
     * 保存临时用工明细
     */
    @PostMapping("/save")
    public TableDataInfo savetemporarywork(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        //部门代码
        factWorkerUsed.setCompanyid(map.get("companyid").toString());
        //日期
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        date = formatter.parse(map.get("planworkdatetime").toString().replace("/","-"));
        factWorkerUsed.setPlanworkdatetime(date);
        //班次
        if (map.get("pshiftid").toString().equals("1")){
            factWorkerUsed.setPshiftid("白班");
        }
        else if(map.get("pshiftid").toString().equals("2")){
            factWorkerUsed.setPshiftid("夜班");
        }
        else if(map.get("pshiftid").toString().equals("3")){
            factWorkerUsed.setPshiftid("中班");
        }
        else if(map.get("pshiftid").toString().equals("4")){
            factWorkerUsed.setPshiftid("转班(白)");
        }
        else if(map.get("pshiftid").toString().equals("5")){
            factWorkerUsed.setPshiftid("转班(夜)");
        }
        //用工类型
        switch (map.get("workertype").toString())
        {
            case "返工挑选":
                factWorkerUsed.setWorkertype("L001");
                break;
            case "设备临时损坏或停机":
                factWorkerUsed.setWorkertype("L002");
                break;
            case "临时性清洁工作":
                factWorkerUsed.setWorkertype("L003");
                break;
            case "临时离厂":
                factWorkerUsed.setWorkertype("L004");
                break;
            case "试模打样":
                factWorkerUsed.setWorkertype("L006");
                break;
            case "在线巡检":
                factWorkerUsed.setWorkertype("L007");
                break;
            case "其他":
                factWorkerUsed.setWorkertype("L005");
                break;
        }
        //实际用工人数
        factWorkerUsed.setFactworksused(Convert.toInt(map.get("factworksused")));
        //实际用工工时
        factWorkerUsed.setWorkhour(Convert.toFloat(map.get("workhour")));
        //创建人
        factWorkerUsed.setCreator(map.get("creator").toString());
        //创建时间
        Date now = new Date();
        factWorkerUsed.setCreatedatetime(now);
        //审核状态
        factWorkerUsed.setChecked(0);
        //图片地址
        //去除特殊字符
        String filesource = map.get("picList").toString();
        filesource=filesource.replace("[","");
        filesource=filesource.replace("]","");
        factWorkerUsed.setFilesource(filesource);

        factWorkerUsedService.insertFactWorkerUsed(factWorkerUsed);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("保存成功");
        return tableDataInfo;
    }

    /**
     * 更新临时用工明细
     */
    @PostMapping("/update")
    public TableDataInfo updatetemporarywork(@RequestBody Map<String, Object> map) throws ParseException {
        FactWorkerUsed factWorkerUsed= new FactWorkerUsed();
        factWorkerUsed.setId(Convert.toLong(map.get("id").toString()));
        factWorkerUsed.setFactworksused(Convert.toInt(map.get("factworksused").toString()));
        factWorkerUsed.setWorkhour(Convert.toFloat(map.get("workhour").toString()));

        int i = factWorkerUsedService.updateFactWorkerUsed(factWorkerUsed);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("更新成功");
        return tableDataInfo;
    }
    /**
     * 删除实际用工明细
     */
    @PostMapping("/delete")
    public TableDataInfo deletetemporarywork(@RequestBody Map<String, Object> map) throws ParseException {
        int i = factWorkerUsedService.deleteFactWorkerUsedById(Convert.toLong(map.get("id").toString()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("删除成功");
        return tableDataInfo;
    }

}
